package com.ruoyi.mall.product.interfaces.facade;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mall.product.domain.service.IPmsCategoryService;
import com.ruoyi.mall.product.domain.vo.TreeSelect;
import com.ruoyi.mall.product.service.PmsCategoryAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mall.product.domain.entity.PmsCategory;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 商品三级分类Controller
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/category")
public class PmsCategoryController extends BaseController
{
    @Autowired
    private PmsCategoryAppService pmsCategoryAppService;

    /**
     * 查询商品三级分类列表,树形结构
     */
    @RequiresPermissions("product:category:list")
    @GetMapping("/list/tree")
    public AjaxResult list()
    {
       List<TreeSelect> pmsCategoryList= pmsCategoryAppService.listWithTree();
        return AjaxResult.success(pmsCategoryList);
    }

    /**
     * 导出商品三级分类列表
     */
    @RequiresPermissions("product:category:export")
    @Log(title = "商品三级分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsCategory pmsCategory)
    {
        List<PmsCategory> list = pmsCategoryAppService.selectPmsCategoryList(pmsCategory);
        ExcelUtil<PmsCategory> util = new ExcelUtil<PmsCategory>(PmsCategory.class);
        util.exportExcel(response, list, "商品三级分类数据");
    }

    /**
     * 获取商品三级分类详细信息
     */
    @RequiresPermissions("product:category:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pmsCategoryAppService.selectPmsCategoryById(id));
    }

    /**
     * 新增商品三级分类
     */
    @RequiresPermissions("product:category:add")
    @Log(title = "商品三级分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsCategory pmsCategory)
    {
        return toAjax(pmsCategoryAppService.insertPmsCategory(pmsCategory));
    }

    /**
     * 批处理三级分类
     * @param pmsCategory
     * @return
     */
    @PutMapping("update/sort")
    public AjaxResult editBatch(@RequestBody PmsCategory[] pmsCategory){

        return toAjax(pmsCategoryAppService.updateBatchById(Arrays.asList(pmsCategory)));
    }

    /**
     * 修改商品三级分类
     */
    @RequiresPermissions("product:category:edit")
    @Log(title = "商品三级分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsCategory pmsCategory)
    {
        return toAjax(pmsCategoryAppService.updatePmsCategory(pmsCategory));
    }

    /**
     * 删除商品三级分类
     * @RequestBody: 获取请求体，只能发POST请求
     */
    @RequiresPermissions("product:category:remove")
    @Log(title = "商品三级分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
//        pmsCategoryService.deleteCategoryByIds(Arrays.asList(ids));
        return toAjax(pmsCategoryAppService.deletePmsCategoryByIds(ids));
    }
}
